﻿Imports TyranntEngine
Imports System.IO

Public Class selectGuildForm

    Private _token As String
    Private _char As New PlayerCharacter
    Private WithEvents _client As New TyranntOnlineCharacterService.TyranntCharacterServiceClient
    Private _guildTable As New Hashtable
    Private _currentGuild As New CharacterGuild
    Private _currentClass As New CharacterClass

    Public Sub Init(ByVal token As String, ByVal charXML As String)
        _token = token
        _char.LoadXML(charXML)
        _client.GetGuildsAsync(_token)
    End Sub

    Private Sub selectGuild_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = _char.Name + " Please select a Guild"
    End Sub

    Private Sub guildNameComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guildNameComboBox.SelectedIndexChanged
        If guildNameComboBox.Text <> "" Then
            guildNameComboBox.Enabled = False
            _client.GetGuildAsync(_token, Integer.Parse(CStr(_guildTable(guildNameComboBox.Text))))
        End If
    End Sub

    Private Sub _client_GetGuildCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetGuildCompletedEventArgs) Handles _client.GetGuildCompleted
        If e.Result.Contains("[#]") Then
            FatalError(e.Result)
        End If
        _currentGuild.LoadXML(e.Result)
        _char.GuildID = _currentGuild.ID
        _client.GetClassAsync(_token, _currentGuild.InitialClassID)
    End Sub

    Private Sub _client_GetClassCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetClassCompletedEventArgs) Handles _client.GetClassCompleted
        If e.Result.Contains("[#]") Then
            FatalError(e.Result)
        End If
        guildNameComboBox.Enabled = True
        _currentClass.LoadXML(e.Result)
        _char.ClassID = _currentClass.ID
        RefreshForm()
    End Sub

    Private Sub RefreshForm()
        guildDescriptionTextBox.Text = _currentGuild.Description
        classNameTextBox.Text = _currentClass.Name
        classDescriptionTextBox.Text = _currentClass.Description
        CheckValid()
    End Sub

    Private Sub _client_GetGuildsCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetGuildsCompletedEventArgs) Handles _client.GetGuildsCompleted
        If e.Result.Contains("[#]") Then
            FatalError(e.Result)
        End If
        guildNameComboBox.Items.Clear()
        Dim sr As New StringReader(e.Result)
        Dim guildsXML As XElement = XElement.Load(sr)
        Dim guilds = guildsXML...<guild>
        For Each aGuild In guilds
            Dim guildID As String = aGuild.@id
            Dim guildName As String = aGuild.<name>.Value
            _guildTable(guildName) = guildID
            guildNameComboBox.Items.Add(guildName)
        Next
        RefreshForm()
    End Sub

    Private Sub FatalError(ByVal message As String)
        MessageBox.Show(message, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End
    End Sub

    Private Sub CheckValid()
        If _char.ClassID = -1 Or _char.GuildID = -1 Then
            nextButton.Enabled = False
        Else
            nextButton.Enabled = True
        End If
    End Sub

    Private Sub nextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextButton.Click
        nextButton.Enabled = False
        _client.SelectGuildAsync(_token, _char.ID, _char.GuildID)
    End Sub

    Private Sub _client_SelectGuildCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.SelectGuildCompletedEventArgs) Handles _client.SelectGuildCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Select Guild Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If

        ' New character worked so now launch next form
        Me.Hide()
        Dim finish As New FinishCharacterForm
        finish.Init(_token, e.Result)
        finish.ShowDialog()
        Me.Close()
    End Sub

    Private Sub canceledButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles canceledButton.Click
        canceledButton.Enabled = False
        nextButton.Enabled = False
        _client.DeleteCharacterAsync(_token, _char.ID)
    End Sub

    Private Sub _client_DeleteCharacterCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.DeleteCharacterCompletedEventArgs) Handles _client.DeleteCharacterCompleted
        If e.Result <> "" Then
            MessageBox.Show(e.Result, "Delete Character Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Me.Close()
    End Sub
End Class